home *** CD-ROM | disk | FTP | other *** search
/ Java Internet Programming Reference Guide / Java Internet Programming Reference Guide.iso / autorun / java.dir / 00199_15.txt < prev    next >
Encoding:
Text File  |  1996-01-12  |  6.4 KB  |  175 lines

  1. Navigator Objects
  2.  
  3.  
  4.  
  5. ΓÇóUsing Navigator Objects ΓÇóNavigator Object Hierarchy ΓÇóKey Navigator Objects 
  6.  
  7.  
  8.  
  9. Using Navigator Objects
  10.  
  11.  
  12.  
  13. When you load a page in Navigator, it creates a number of objects corresponding to the page, its contents, and other pertinent information. 
  14.  
  15. Every page always has the following objects: 
  16.  
  17. ΓÇówindow: the top-level object; contains properties for "child windows" in frames (if any) ΓÇólocation: contains properties on the current URL ΓÇóhistory: contains properties representing URLs the user has previously visited ΓÇódocument: contains properties for content in the current document, such as title, background color, and forms 
  18.  
  19.  
  20.  
  21. The properties of the document object are largely content-dependent. That is, they are created based on the content that you put in the document. For example, the document object has a property for each form and each anchor in the document. 
  22.  
  23. For example, suppose you create a page named simple.html that contains the following HTML: 
  24.  
  25.  
  26. <TITLE>A Simple Document</TITLE>
  27. <BODY>
  28.  
  29. <FORM NAME="myform" ACTION="FormProc()" METHOD="get" >
  30.  
  31. Enter a value: <INPUT TYPE=text NAME="text1" VALUE="blahblah" SIZE=20 >
  32. Check if you want: 
  33. <INPUT TYPE="checkbox" NAME="Check1" CHECKED onClick="update(this.form)"> Option #1
  34. <P>
  35. <INPUT TYPE="button" NAME="Button1" VALUE="Press Me" onClick="update(this.form)">
  36. </FORM></BODY>
  37.  
  38.  
  39.  
  40.  
  41.  
  42. As always, there would be window, location, history, and document objects. These would have properties such as: 
  43.  
  44. ΓÇólocation.href = "http://www.terrapin.com/samples/vsimple.html" ΓÇódocument.title = "A Simple Document" ΓÇódocument.fgColor = #000000 ΓÇódocument.bgColor = #ffffff ΓÇóhistory.length = 7 
  45.  
  46.  
  47.  
  48. These are just some example values. In practice, these values would be based on the document's actual location, its title, foreground and background colors, and so on. 
  49.  
  50. Navigator would also create the following objects based on the contents of the page: 
  51.  
  52. ΓÇódocument.myform ΓÇódocument.myform.Check1 ΓÇódocument.myform.Button1 
  53.  
  54.  
  55.  
  56. These would have properties such as: 
  57.  
  58. ΓÇódocument.myform.action = http://terrapin/mocha/formproc() ΓÇódocument.myform.method = get ΓÇódocument.myform.length = 5 ΓÇódocument.myform.Button1.value = Press Me ΓÇódocument.myform.Button1.name = Button1 ΓÇódocument.myform.text1.value = blahblah ΓÇódocument.myform.text1.name = text1 ΓÇódocument.myform.Check1.defaultChecked = true ΓÇódocument.myform.Check1.value = on ΓÇódocument.myform.Check1.name = Check1 
  59.  
  60.  
  61.  
  62. Notice that each of the property references above starts with "document," followed by the name of the form, "myform," and then the property name (for form properties) or the name of the form element. This sequence follows the Navigator's object hierarchy, discussed in the next section. 
  63.  
  64. Navigator Object Hierarchy
  65.  
  66.  
  67.  
  68. The objects in Navigator exist in a hierarchy that reflects the hierarchical structure of the HTML page itself. Although you cannot derive object classes from these objects, as you can in languages such as Java, it is useful to understand the Navigator's JavaScript object hierarchy. In the strict object-oriented sense, this type of hierarchy is known as an instance hierarchy, since it concerns specific instances of objects rather than object classes. 
  69.  
  70. In this hierarchy, an object's "descendants" are properties of the object. For example, a form named "form1" is an object, but is also a property of document, and is referred to as "document.form1". The Navigator object hierarchy is illustrated below: 
  71.  
  72.  
  73. window 
  74.   |
  75.   +--parent, frames, self, top 
  76.   |
  77.   +--location 
  78.   | 
  79.   +--history
  80.   |
  81.   +--document
  82.        |
  83.        +--forms
  84.        |    |
  85.        |  elements (text fields, textareas, checkboxes, 
  86.        |            radio buttons, selections, buttons)   
  87.        +--links
  88.        |
  89.        +--anchors
  90.        |
  91.        +--applets (NYI)
  92.  
  93.  
  94.  
  95.  
  96.  
  97. To refer to specific properties of these objects, you must specify the object name and all its ancestors. Exception: You are not required to include the window object. 
  98.  
  99. Key Navigator Objects
  100.  
  101.  
  102.  
  103. Some of the most useful Navigator objects include document, form, and window. 
  104.  
  105. Using the document Object
  106.  
  107.  
  108.  
  109. One of the most useful Navigator objects is the document object, because its write and writeln methods can generate HTML. These methods are the way that you display JavaScript expressions to the user. The only difference between write and writeln is that writeln adds a carriage return at the end of the line. However, since HTML ignores carriage returns, this will only affect preformatted text, such as that inside a PRE tag. 
  110.  
  111. The document object also has onLoad and onUnload event-handlers to perform functions when a user first loads a page and when a user exits a page. 
  112.  
  113. There is only one document object for a page, and it is the ancestor for all the form, link, and anchor objects in the page. 
  114.  
  115. Using the form Object
  116.  
  117.  
  118.  
  119. Navigator creates a form object for each form in a document. You can name a form with the NAME attribute, as in this example: 
  120.  
  121.  
  122. <FORM NAME="myform">
  123. <INPUT TYPE="text" NAME="quantity" onChange="...">
  124. ...
  125. </FORM>
  126.  
  127.  
  128.  
  129.  
  130.  
  131. There would be a JavaScript object named myform based on this form. The form would have a property corresponding to the text object, that you would refer to as 
  132.  
  133.  
  134. document.myform.quantity
  135.  
  136.  
  137.  
  138. You would refer to the value property of this object as 
  139.  
  140.  
  141. document.myform.quantity.value
  142.  
  143.  
  144.  
  145.  
  146.  
  147. The forms in a document are stored in an array called forms. The first (topmost in the page) form is forms[0], the second forms[1], and so on. So the above references could also be: 
  148.  
  149.  
  150. document.forms[0].quantity
  151. document.forms[0].quantity.value
  152.  
  153.  
  154.  
  155.  
  156.  
  157. Likewise, the elements in a form, such as text fields, radio buttons, and so on, are stored in an elements array. 
  158.  
  159. Using the window Object
  160.  
  161.  
  162.  
  163. The window object is the "parent" object for all other objects in Navigator. You can always omit the object name in references to window properties and methods. 
  164.  
  165. Window has several very useful methods that create new windows and pop-up dialog boxes: 
  166.  
  167. ΓÇóopen and close: Opens and closes a browser window ΓÇóalert: Pops up an alert dialog box ΓÇóconfirm: Pops up a confirmation dialog box 
  168.  
  169.  
  170.  
  171. The window object has properties for all the frames in a frameset. The frames are stored in the frames array. The frames array contains an entry for each child frame in a window. For example, if a window contains three child frames, these frames are reflected as window.frames[0], window.frames[1], and window.frames[2]. 
  172.  
  173. The status property enables you to set the message in the status bar at the bottom of the client window. 
  174.  
  175.